package com.base.cn.platform.os.controller.manage.subject;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.service.manage.subject.SubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 后台专业/科目Controller
 *
 * @author s.li
 * @create 2018-03-20 15:32
 */
@Controller
@RequestMapping("/admin/manage/subject")
public class AdminSubjectController extends AdminBaseController {

    @Autowired
    private SubjectService subjectService;
    /**
     * 初始化专业列表
     * @return 列表显示页面
     */
    @RequestMapping("/toSubjectList")
    public String toSubjectList(){
        return resultPage("/manage/subject/subject-list");
    }

    /**
     * 查询专业数据
     * @return Map<String,Object>
     */
    @RequestMapping("/ajax/ajaxSubjectList")
    @ResponseBody
    public Map<String,Object> ajaxSubjectList(){
        Map<String,Object> params = new HashMap<>();
        List<Map<String,Object>> subjectList = subjectService.findSubjectList(params);
        return ResultUtil.SUCCESS("专业查询成功",subjectList);
    }

    /**
     * 初始化修改添加页面
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = {"/initUpdate","/initAdd"})
    public String initUpdateOrAdd(Model model,
                                  @RequestParam(value = "parentId",required = false,defaultValue = "0") BigDecimal parentId,
                                  @RequestParam(value = "id",required = false,defaultValue = "0")BigDecimal id){
        Map<String,Object> subject = null;
        if(id.compareTo(new BigDecimal(0))>0){
            subject = subjectService.findSubjectById(id);
        }
        if(subject==null){
            subject = new HashMap<>();
            subject.put("parentId",parentId);
        }
        model.addAttribute("subject",subject);
        return resultPage("/manage/subject/save-subject");
    }

    /**
     * 保存专业/科目
     * @param request
     * @param subject
     * @return
     */
    @RequestMapping("/saveSubject")
    @ResponseBody
    public Map<String,Object> saveSubject(HttpServletRequest request, @RequestBody Map<String,Object> subject){
        subject.put("createUserId",loginUtil.getLoginUserId(request));
        StringBuffer placeKeys = new StringBuffer();
        String placeKeyOne = (String) subject.remove("placeKeyOne");
        String placeKeyTwo = (String) subject.remove("placeKeyTwo");
        if(StringUtils.isNotEmpty(placeKeyOne)){
            placeKeys.append(placeKeyOne);
        }
        if(StringUtils.isNotEmpty(placeKeyTwo)){
            placeKeys.append(",").append(placeKeyTwo);
        }
        if(StringUtils.isNotEmpty(placeKeys.toString())){
            subject.put("placeKeys",placeKeys.toString());
        }
        Map<String,Object> resultMap = subjectService.saveSubject(subject);
        if((boolean)resultMap.get("success")==true ){
            String strParentId = (String)subject.get("parentId");
            if(StringUtils.isNotEmpty(strParentId)){
                BigDecimal parentId = new BigDecimal(strParentId);
                resultMap.put("parentId",parentId);
            }
        }
        return resultMap;
    }

    /**
     * 修改专业的父级专业
     * @param subjectId 专业ID
     * @param parentId  父级ID
     * @return Map<String,Object>
     */
    @RequestMapping("/updateSubjectParentId")
    @ResponseBody
    public Map<String,Object> updateSubjectParentId(BigDecimal subjectId,BigDecimal parentId){
        return subjectService.updateSubjectParentId(subjectId,parentId);
    }

    /**
     * 修改专业状态
     * @param ids 专业ID串
     * @param status 状态，1正常，2禁用，3删除
     * @return Map<String,Object>
     */
    @RequestMapping("/updateSubjectStatus")
    @ResponseBody
    public Map<String,Object> updateSubjectStatus(@RequestParam("ids") String ids,@RequestParam("status") int status){
        return subjectService.updateSubjectStatus(ids,status);
    }

    @RequestMapping("/findLayeredSubjectList")
    @ResponseBody
    public Map<String,Object> findLayeredSubjectList(){
        Map<String,Object> params = new HashMap<>();
        params.put("status",1);//查询状态正常的
        Map<String,List<Map<String,Object>>> subjectMapList = subjectService.findSubjectListMap(params);
        return ResultUtil.SUCCESS("专业获取成功",subjectMapList);
    }


    /**
     * 调整节点排序
     * @param currentId 要操作的节点ID
     * @param currentSort 操作的节点的新排序
     * @param oldId 被替换节点的ID
     * @param oldSort 被替换节点的排序
     * @return Map<String,Object>
     */
    @RequestMapping("/updateNodeSort")
    @ResponseBody
    public Map<String,Object> updateNodeSort(@RequestParam("currentId") BigDecimal currentId,
                                             @RequestParam("currentSort") int currentSort,
                                             @RequestParam("oldId") BigDecimal oldId,
                                             @RequestParam("oldSort") int oldSort){
        subjectService.updateNodeSort(currentId,currentSort,oldId,oldSort);
        return ResultUtil.SUCCESS();
    }

}
